﻿Imports Microsoft.VisualBasic

Namespace ClubStarterKit.Web.Controls
    Public Class TimePicker
        Inherits WebControl
        Implements INamingContainer

        Private _innerTbx As TextBox
        Private _innerList As ListBox
        Private errorText As String = Nothing

        Sub New()
            MyBase.New(UI.HtmlTextWriterTag.Div)
        End Sub

        Public Property SelectedTime() As Date
            Get
                EnsureChildControls()
                Dim d As Date
                Try
                    d = Date.Parse(_innerTbx.Text)
                    errorText = Nothing
                Catch
                    errorText = "Date needs to be specified as hh:mm"
                End Try
                Return d
            End Get
            Set(ByVal value As Date)
                EnsureChildControls()
                Dim s As String = value.ToString("h:mm tt")
                _innerTbx.Text = s
                Dim item As ListItem = _innerList.Items.FindByText(s)
                If Not item Is Nothing Then _innerList.SelectedValue = s
            End Set
        End Property

        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()
            _innerTbx = New TextBox
            Me.Controls.Add(_innerTbx)

            _innerList = New ListBox
            FillTimes()

            Controls.Add(_innerList)
        End Sub

        Sub FillTimes()
            If _innerList.Items.Count = 0 Then
                For i As Integer = 6 To 11
                    _innerList.Items.Add(i & ":00 AM")
                    _innerList.Items.Add(i & ":30 AM")
                Next
                _innerList.Items.Add("12:00 PM")
                _innerList.Items.Add("12:30 PM")
                For i As Integer = 1 To 11
                    _innerList.Items.Add(i & ":00 PM")
                    _innerList.Items.Add(i & ":30 PM")
                Next
            End If
        End Sub


        Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
            Get
                Return HtmlTextWriterTag.Div
            End Get
        End Property


        Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)
            If Me.Width.IsEmpty Then Me.Width = New Unit(150)
            Me.Style.Add("display", "inline-table")
            MyBase.AddAttributesToRender(writer)
        End Sub

        Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)

            Dim listid As String = _innerList.ClientID
            Dim tbxid As String = _innerTbx.ClientID

            _innerTbx.Attributes.Add("Align", "AbsMiddle")
            _innerTbx.Width = New Unit(100)
            _innerTbx.RenderControl(writer)

            writer.AddAttribute("Align", "AbsMiddle")
            writer.AddAttribute("src", "../App_Themes/" & Page.Theme.ToString() & "/images/dropdownbtn.gif")
            writer.AddAttribute("onClick", "__timepicker_showpopup('" & listid & "')")
            writer.RenderBeginTag(HtmlTextWriterTag.Img)
            writer.RenderEndTag()

            If errorText <> Nothing Then
                writer.AddStyleAttribute("color", "red")
                writer.AddStyleAttribute("display", "block")
                writer.RenderBeginTag(HtmlTextWriterTag.Span)
                writer.Write(errorText)
                writer.RenderEndTag()
            End If

            writer.AddStyleAttribute("position", "relative")
            writer.RenderBeginTag(HtmlTextWriterTag.Div)

            _innerList.Width = New Unit(120)
            _innerList.Rows = 8
            _innerList.Style.Add("position", "absolute")
            _innerList.Style.Add("left", "0px")
            _innerList.Style.Add("display", "none")
            _innerList.Attributes.Add("onfocusout", "__popup_losefocus(this);")
            _innerList.Attributes.Add("onchange", "__timepicker_timechanged('" & tbxid & "',this);")

            _innerList.RenderControl(writer)

            writer.RenderEndTag()
        End Sub

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)

            commonScript.WritePopupRoutines(Page)

            Dim sb As New StringBuilder
            sb.AppendLine("function __timepicker_showpopup(name)")
            sb.AppendLine("{")
            sb.AppendLine("     if (__popup_panel != null)")
            sb.AppendLine("     {")
            sb.AppendLine("         document.getElementById(__popup_panel).style.display='none';")
            sb.AppendLine("     }")
            sb.AppendLine("     __popup_panel=name;")
            sb.AppendLine("     var panel=document.getElementById(__popup_panel);")
            sb.AppendLine("     panel.style.display='block';")
            sb.AppendLine("     panel.focus();")
            sb.AppendLine("     window.event.cancelBubble=true;")
            sb.AppendLine("}")

            sb.AppendLine("function __timepicker_timechanged(tbxid, selectid)")
            sb.AppendLine("{")
            sb.AppendLine("document.getElementById(tbxid).value=selectid.options[selectid.selectedIndex].text;")
            sb.AppendLine("     if (__popup_panel != null)")
            sb.AppendLine("     {")
            sb.AppendLine("         document.getElementById(__popup_panel).style.display='none';")
            sb.AppendLine("         __popup_panel=null;")
            sb.AppendLine("     }")
            sb.AppendLine("}")

            Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "popup", sb.ToString, True)

        End Sub

    End Class
End Namespace